//
//  XMDiscountListViewController.m
//  XunMi
//
//  Created by MacBook on 2024/4/16.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMDiscountListViewController.h"
#import "XMDiscountListTableViewCell.h"
#import "XMDiscountsModel.h"
#import "XMDiscountListHeaderView.h"

@interface XMDiscountListViewController ()
@property (nonatomic, strong) NSArray <XMDiscountsModel *> *usableArray;
@property (nonatomic, strong) NSArray <XMDiscountsModel *> *invalidArray;
@end

@implementation XMDiscountListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initVariables];
    [self setupSubviews];
}

- (void)setDataUsableArray:(NSArray<XMDiscountsModel *> *)usableArray invalidArray:(NSArray<XMDiscountsModel *> *)invalidArray {
    self.usableArray = usableArray;
    self.invalidArray = invalidArray;
    [self.tableView reloadData];
}

#pragma mark - 初始化变量
- (void)initVariables {
    [self tableViewRegisterNibWithNibName:kStringFromClass(XMDiscountListTableViewCell)];
}

#pragma mark - setupSubViews
- (void)setupSubviews {
    self.view.backgroundColor = kHEX(@"#F2F2F2");
    self.navView.backgroundColor = kHEX(@"#F2F2F2");
    self.tableView.backgroundColor = kHEX(@"#F2F2F2");
    [self.navLeft2Btn setTitle:@"优惠卡" forState:UIControlStateNormal];
    
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(kNavBarAndStatusBarHeight, 0, kBottomSafeHeight, 0));
    }];
}

#pragma mark - viewBlock
- (void)viewBlock {
    
}
#pragma mark - Actions
#pragma mark - Public Methods
#pragma mark - Privacy Methods

#pragma mark - UITableViewDataSource & UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.usableArray.count;
    }
    return self.invalidArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 110;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XMDiscountListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XMDiscountListTableViewCell class])];
    if (indexPath.section == 0) {
        cell.coverView.hidden = YES;
        if (self.usableArray.count > 0) {
            cell.model = self.usableArray[indexPath.row];
        }
    } else {
        cell.coverView.hidden = NO;
        if (self.invalidArray.count > 0) {
            cell.model = self.invalidArray[indexPath.row];
        }
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 1 && self.invalidArray.count == 0) {
        return CGFLOAT_MIN;
    }
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headV = [UIView new];
    headV.backgroundColor = kHEX(@"#F2F2F2");

    XMDiscountListHeaderView *header = [XMDiscountListHeaderView viewFromXib];
    header.backgroundColor = kHEX(@"#F2F2F2");
    header.titleLa.text = section == 0 ? @"可使用卡券" : @"不可使用卡券";
    header.countLa.text = section == 0 ? kStringFormat(@"%ld张", self.usableArray.count) : kStringFormat(@"%ld张", self.invalidArray.count);
    header.wentiBtn.hidden = section == 1;

    [headV addSubview:header];
    [header mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(headV);
    }];
    if (section == 1 && self.invalidArray.count == 0) {
        return nil;
    }
    return headV;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        XMDiscountsModel *model = [self.usableArray safe_objectAtIndex:indexPath.row];
        if (model.selected) {
            if (self.selBlock) {
                self.selBlock(nil);
            }
        } else {
            if (self.selBlock) {
                self.selBlock(model);
            }
        }
        
        [self backAction];
    }
}

@end
